package com.cictec.middleware.core.controller;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cictec.middleware.commons.utils.DateUtils;
import com.cictec.middleware.commons.utils.Result;
import com.cictec.middleware.commons.utils.ResultUtil;
import com.cictec.middleware.core.entity.Bus;
import com.cictec.middleware.core.entity.BusDriver;
import com.cictec.middleware.core.entity.BusDriverSignIn;
import com.cictec.middleware.core.pojo.MiddlewareDriverSign;
import com.cictec.middleware.core.service.BusDriverService;
import com.cictec.middleware.core.service.BusService;

@RestController
public class BusDriverController {

	private static Logger logger = LoggerFactory.getLogger(BusDriverController.class);

	@Autowired
	private BusDriverService busDriverService;
	@Autowired
	private BusService busService;

	/**
	 * 【数据库】获取所有车辆信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "/dc/driver/getBusDriverSignIn")
	public List<BusDriverSignIn> getBusDriverSignIn(@RequestParam("busUuid") String busUuid,
			@RequestParam("date") String date) {

		logger.debug("查询司机签到信息 {}-{}", busUuid, date);
		return busDriverService.selectSignByBusUuid(busUuid, date);
	}

	/**
	 * 【数据库】司机签到
	 * 
	 * @return
	 */
	@RequestMapping(value = "/dc/driver/driverSign")
	public Result driverSign(@RequestBody MiddlewareDriverSign middlewareDriverSign) {
		Date signTime = middlewareDriverSign.getSignTime();
		String devCode = middlewareDriverSign.getDevCode();
		String icCardNumber = middlewareDriverSign.getCardNumber();
		System.out.println("收到的时间："+ DateUtils.formatDateTimes(middlewareDriverSign.getSignTime()));
		Bus bus = busService.selectBusByDevCode(middlewareDriverSign.getDevCode());
		
		if(bus != null){
			BusDriver driver = busDriverService.selectDriverByIC(icCardNumber);
			if(driver != null){
				busDriverService.insertBusDirverSignIn(driver, bus, bus.getBusWorkingLineUuid(), signTime);
//				return busService.selectListAllOnlineBus();
				return ResultUtil.getSuccessResult();
			}
			return ResultUtil.getErrorResult("找不到 icCardNumber:{} 对应的司机, 司机刷卡信息无效", icCardNumber);
			
		}else{
			return ResultUtil.getErrorResult("找不到 devCode:{} 对应的车辆, 司机刷卡信息无效", devCode);
		}
	}
	
	@RequestMapping(value = "/dc/driver/getDriverByIcCardNumber")
	public BusDriver getDriverByIcCardNumber(@RequestParam("icCardNumber") String icCardNumber){
		BusDriver busDriver = busDriverService.selectDriverByIC(icCardNumber);
		return busDriver;
	}

}
